//
//  AdressChangeViewController.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/8.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "AdressChangeViewController.h"
#import "BaseTableViewCell.h"
#import "UITableView+SCIndexView.h"
#import "SectionItem.h"
@interface AdressChangeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, copy) NSArray<SectionItem *> *tableViewDataSource;

@property (nonatomic, assign) BOOL translucent;
@end

@implementation AdressChangeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.translucent = YES;
    self.title = @"城市选择";
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.mj_footer.hidden = YES;
    self.tableView.mj_header.hidden = YES;
    self.tableView.rowHeight = kHeightProportion_In_iPhoneTriploid(44);
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(86))];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(32))];
    [self.view addSubview:self.tableView];
    //
    SCIndexViewConfiguration *configuration = [SCIndexViewConfiguration configurationWithIndexViewStyle:self.indexViewStyle];
    configuration.indexItemTextColor = kColor_Rule_Yellow;
    self.tableView.sc_indexViewConfiguration = configuration;
    self.tableView.sc_translucentForTableViewInNavigationBar = self.translucent;
    //
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Indexes" ofType:@"plist"];
        NSArray<SectionItem *> *tableViewDataSource = [NSArray yy_modelArrayWithClass:SectionItem.class json:[NSArray arrayWithContentsOfFile:plistPath]];
        
        NSMutableArray *indexViewDataSource = [NSMutableArray array];
        for (SectionItem *item in tableViewDataSource) {
            [indexViewDataSource addObject:item.title];
        }
        dispatch_sync(dispatch_get_main_queue(), ^{
            self.tableViewDataSource = tableViewDataSource.copy;
            [self.tableView reloadData];
            self.tableView.sc_indexViewDataSource = indexViewDataSource.copy;
        });
    });
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
}

#pragma mark =================== UITableViewDelegate,UITableViewDataSource ===================
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.tableViewDataSource.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    SectionItem *sectionItem = self.tableViewDataSource[section];
    return sectionItem.items.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BaseTableViewCell * cell = [BaseTableViewCell cellWithTableView:tableView];
    SectionItem *sectionItem = self.tableViewDataSource[indexPath.section];
    cell.textLabel.text = sectionItem.items[indexPath.row];;
    [cell setSepartionLineWith:lineModel_Half];
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    SectionItem *sectionItem = self.tableViewDataSource[section];
    return sectionItem.title;
}

@end
